<template>
  <div>
    <el-form ref="giftOrderForm" size="small" :model="entity" :rules="rules" label-width="120px">
      <el-form-item label="订单礼品" prop="giftname">
        <el-input v-model="entity.giftname" disabled placeholder="订单礼品" >
          <template slot="prepend"><span @click="giftSelectClick">选择</span></template>
        </el-input>
      </el-form-item>
      <el-form-item label="礼品模板" v-show="giftModelFileList.length > 0">
        <div v-for="item in giftModelFileList" :key="item.fId" style="display: inline-block;text-align: center">
          <el-image :src="item.url" :preview-src-list="item.prevSrc" :class="{active : item.fId === entity.modelFileid}"
                    style="width: 160px; height: 160px;margin: 4px;"  fit="fill">
          </el-image>
          <br/>
          <el-button size="mini" @click="modelSelectCb(item.fId)">选择</el-button>
        </div>
      </el-form-item>
      <el-form-item label="订单数量" prop="shuliang">
        <el-input type="number" v-model="entity.shuliang" placeholder="需要数量" />
      </el-form-item>
      <el-form-item label="需求日期" prop="requireDate">
        <el-date-picker v-model="entity.requireDate" type="date" placeholder="需求日期" style="width: 100%;" value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="订单描述" prop="descption">
        <el-input type="textarea" v-model="entity.descption" :rows="4" placeholder="订单描述" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </el-form-item>
    </el-form>

    <el-dialog title="礼品选择" :visible.sync="giftModelVisible" append-to-body :close-on-click-modal="false" width="1360">
      <GiftSelect v-if="giftModelVisible" :visible.sync="giftModelVisible" @selected="giftSelectCb"></GiftSelect>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
import dayjs from "dayjs"
import GiftSelect from "@/views/gift/GiftSelectV2";
import FileEntityType from "../../utils/FileEntityType";
import {baseURL} from '@/config';
export default {
  components:{GiftSelect},
  props:{
    entityId : {type : Number, default: 0},
  },
  data() {
    return {
      entity: {
        id : this.entityId,
        shuliang: 1,
        giftId:0,
        giftname:'',
        modelFileid:0,
        requireDate:'',
        descption:'',
      },
      rules: {
        giftname: [
          { required: true, message: '请选择订单礼品', trigger: 'blur' }
        ],
        shuliang: [
          { required: true, message: '请输入订单数量', trigger: 'blur' }
        ],
        requireDate: [
          { required: true, message: '请输入需求日期', trigger: 'blur' }
        ]
      },

      giftModelVisible : false,
      giftModelFileList:[]
    }
  },
  mounted() {
    this.loadEntity();
  },
  methods: {
    giftSelectClick(){
      this.giftModelVisible = true
    },
    giftSelectCb(giftObj){
      this.entity.giftId=giftObj.id
      this.entity.giftname=giftObj.name + "("+giftObj.guige+")"
      this.loadGiftModelImage(giftObj.id);
    },
    loadGiftModelImage(id){
      this.giftModelFileList=[];
      request({
        url: '/common/listfile/' + FileEntityType.giftmodel + "/" + id,
        method: 'get'
      }).then((res)=>{
        if(res.data && Array.isArray(res.data)){
          res.data.forEach(f=>{
            this.giftModelFileList.push({name: f.filePath, url: baseURL + f.filePath, fId : f.fId, prevSrc:[baseURL + f.filePath] })
          })
        }
      })
    },
    modelSelectCb(fId){
      this.entity.modelFileid=fId
    },
    loadEntity(){
      if(this.entityId > 0){
        request({ url: '/gift/order/getgiftorder', data: {id : this.entityId} }).then( res =>{
          this.entity = res.data
          this.entity.requireDate = dayjs(this.entity.requireDate).format('YYYY-MM-DD')
          this.entity.giftname = this.entity.giftname+"("+this.entity.giftguige+")"
          if(this.entity.giftId){
            this.loadGiftModelImage(this.entity.giftId);
          }
        })
      }
    },
    onSubmit(){
      this.$refs['giftOrderForm'].validate((valid) => {
        if(valid){
          if(this.giftModelFileList.length>0 && !this.entity.modelFileid){
            this.$message.error("请选择一个礼品模板");
            return
          }
          request({
            url: '/gift/order/addgiftorder',
            data: this.entity
          }).then( res =>{
            if(res.code === "200"){
              this.$message.success(res.msg)
              this.$emit('update:visible', false);  //关闭弹窗
              //触发事件总线，让列表界面更新数据
              this.entityId > 0 ? this.$EventBus.$emit("orderedit", {}):this.$EventBus.$emit("orderadd", {})
            }else{
              this.$message.error(res.msg);
            }
          })
        }else{
          console.log('error submit!!');
          return false;
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.active{
  border: 2px solid #ff430d;
}
</style>
